package com.bdqn.config.shiro;

import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import jakarta.annotation.Resource;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class MyShiroRealm extends AuthorizingRealm {

    @Resource
    private SysUserService sysUserService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("调试myShiroRealm.doGetAuthorizationInfo()获取权限信息");
        SysUser sysUser = (SysUser) principalCollection.getPrimaryPrincipal();
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo();
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("调试myShiroRealm.doGetAuthenticationInfo()获取身份信息");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String usrName = token.getUsername();
        SysUser user = sysUserService.findUserByUsrName(usrName);
        if (user==null){
            throw  new UnknownAccountException();
        }
        if (user.getUsrFlag()==null||user.getUsrFlag().intValue()==0){
            throw  new LockedAccountException();
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user,user.getUsrPassword(),getName());
        return info;
    }
}
